/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.icon.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import offset.nodes.server.binary.controller.BinaryAttribute;
import offset.nodes.server.controller.AuthenticatingAction;
import offset.nodes.server.icon.model.IconModel;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Walter L�tgenau
 */
public class IconAction extends AuthenticatingAction {
    public static String PAR_NODE_TYPE = "nodeType";
    public static String PAR_PROPERTY_TYPE = "propertyType";
    public static String PAR_PROPERTY_NAME = "propertyName";
    public static String PAR_PROPERTY_VALUE = "propertyValue";

    @Override
    public ActionForward process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        IconForm iconForm = (IconForm) form;
        IconModel iconModel = new IconModel(getRepositorySession(request));
        
        int propertyType = -1;
        try {
            String propertyTypeString = request.getParameter(PAR_PROPERTY_TYPE);
            if (propertyTypeString != null)
                propertyType = Integer.parseInt(propertyTypeString);
        } catch(NumberFormatException e) {}

        BinaryAttribute downloadAttribute = iconModel.getIcon(
                request.getParameter(PAR_NODE_TYPE),
                propertyType,
                request.getParameter(PAR_PROPERTY_NAME),
                request.getParameter(PAR_PROPERTY_VALUE));
        if (downloadAttribute != null)
            request.setAttribute(downloadAttribute.getId(), downloadAttribute);

        return mapping.findForward("binary");
    }

}
